package org.dftproject.pgv.ui;

import org.dftproject.genesis.ui.figures.ButtonFigure;
import org.dftproject.genesis.ui.figures.EditFigure;
import org.dftproject.genesis.ui.figures.FigureCanvasEx;
import org.dftproject.genesis.ui.figures.LinedContainer;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.figures.UniformSizeContainer;
import org.dftproject.genesis.ui.figures.UniformSizeGroup;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.dftproject.pgv.core.racp.PGVGedcom;
import org.dftproject.pgv.core.racp.Session;
import org.dftproject.pgv.core.racp.SessionManager;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.draw2d.ActionEvent;
import org.eclipse.draw2d.ActionListener;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

public class AddSitePage extends AbstractPagePart {

	public static final String ID = AddSitePage.class.getName();
	
	private FigureCanvas canvas;

	public void init(IPageSite site, IPageInput input) throws PageInitException {
		setInput(input);
		setSite(site);
	}

	@Override
	public void createPartControl(Composite parent) {		
		createContents(parent);
	}
	
	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvasEx(parent);

		// Create the root figure

		Figure topContainer = new PageContainer();
		canvas.getViewport().setContentsTracksWidth(true);
		canvas.setContents(topContainer);
		
		topContainer.add(PageUtils.createH1Label("Connect to a PhpGedView Web site"));
		
		topContainer.add(PageUtils.createFlowedText("Enter the connection details for a PhpGedView Web site below:"));
		
		IFigure subsection = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		subsection.setLayoutManager(layout);
		subsection.setBorder(new MarginBorder(10, 10, 30, 0));
		topContainer.add(subsection);
		
		Figure container = new Figure();
		layout = new ToolbarLayout(true);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(true);
		container.setLayoutManager(layout);
		subsection.add(container);
		
		UniformSizeGroup uniformSizeGroup = new UniformSizeGroup();
		
		Label label = new Label("Location:");
		label.setTextPlacement(PositionConstants.NORTH);
		label.setTextAlignment(PositionConstants.RIGHT);
		container.add(new UniformSizeContainer(label, uniformSizeGroup));
		
		final EditFigure urlFigure = new EditFigure(canvas, SWT.BORDER);
		urlFigure.setText("http://");
		container.add(urlFigure);
		
		ButtonFigure selectBtn = new ButtonFigure(canvas, SWT.PUSH);
		selectBtn.setText("Get list of collections");
		container.add(selectBtn);
		
		container = new Figure();
		layout = new ToolbarLayout(true);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(true);
		container.setLayoutManager(layout);
		subsection.add(container);
		
		label = new Label("Username:");
		label.setTextPlacement(PositionConstants.NORTH);
		label.setTextAlignment(PositionConstants.RIGHT);
		container.add(new UniformSizeContainer(label, uniformSizeGroup));
		
		final EditFigure usernameFigure = new EditFigure(canvas, SWT.BORDER);
		container.add(usernameFigure);
		
		label = new Label();
		label.setText("(leave blank for anonymous access)");
		container.add(label);
		
		container = new Figure();
		layout = new ToolbarLayout(true);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(true);
		container.setLayoutManager(layout);
		subsection.add(container);
		
		label = new Label("Password:");
		label.setTextPlacement(PositionConstants.NORTH);
		label.setTextAlignment(PositionConstants.RIGHT);
		container.add(new UniformSizeContainer(label, uniformSizeGroup));
		
		final EditFigure passwordFigure = new EditFigure(canvas, SWT.BORDER | SWT.PASSWORD);
		container.add(passwordFigure);
		
		final Figure collectionHeader = PageUtils.createFlowedText("Select a collection:");
		collectionHeader.setVisible(false);
		topContainer.add(collectionHeader);
		
		final LinedContainer collectionsList = new LinedContainer();
		collectionsList.setBorder(new MarginBorder(10, 10, 30, 0));
		topContainer.add(collectionsList);

		selectBtn.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {
				final String url = urlFigure.getText();
				final String username = usernameFigure.getText();
				final String password = passwordFigure.getText();
				
				Job job = new Job("Get list of collections") {

					@Override
					protected IStatus run(IProgressMonitor monitor) {
						Session session = (!"".equals(username) && !"".equals(password)) ? SessionManager.getDefault().getSession(url, username, password) : SessionManager.getDefault().getSession(url);
						if (session != null) {
							final PGVGedcom[] gedcoms = session.listPGVGedcoms();
							Display.getDefault().asyncExec(new Runnable() {

								public void run() {
									collectionsList.removeAll();
									
									collectionHeader.setVisible(true);
									
									UniformSizeGroup linkGroup = new UniformSizeGroup();
									for (PGVGedcom pGVGedcom : gedcoms) {
										Figure gedcomFigure = new GedcomFigure(pGVGedcom, linkGroup);
										collectionsList.add(gedcomFigure);
									}								
								}
								
							});
						}
						
						return Status.OK_STATUS;
					}
					
				};
				job.schedule();
			}
			
		});
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
